<template>
  <div>
    <!-- 搜索入口 -->
    <div class="search-container" @click="$router.push('/search')">
      <div class="search">
        <van-icon name="sousuoxiao" class-prefix="icon" size="18" />
        <span>搜索商品鲜花xxxx</span>
      </div>
    </div>
    <div class="cate-container">
      <div class="cates-wrap">
        <div class="cates">
          <div
            v-for="(cate, index) in cates"
            :key="cate.id"
            @click="switchTab(index)"
            :class="[
              'cate',
              {
                active: index === activeIndex,
              },
            ]"
          >
            {{ cate.name }}
          </div>
        </div>
      </div>
      <div class="items-wrap">
        <div class="items">
          <div
            class="item"
            @click="clickItem(item.id)"
            v-for="item in items"
            :key="item.name"
          >
            <img :src="item.pic" alt="" class="item-img" />
            <div class="item-info">
              <h5 class="item-name">
                {{ item.name }}
              </h5>
              <p class="item-desc">
                {{ item.characteristic ? item.characteristic : "暂无描述" }}
              </p>
              <div class="item-buy">
                <p class="price">￥{{ item.minPrice }}</p>
                <van-icon
                  name="gouwuche"
                  size="20"
                  color="#ff734c"
                  class-prefix="icon"
                />
              </div>
            </div>
          </div>
        </div>
      </div>
    </div>
    <my-tabbar />
  </div>
</template>

<script>
import MyTabbar from '../../components/MyTabbar.vue'
import { fetchCates, fetchItems } from '_api'
export default {
  components: { MyTabbar },
  name: 'Cate',
  data () {
    return {
      cates: [],
      items: [],
      activeIndex: 0
    }
  },
  created () {
    this.fetchCates()
  },
  methods: {
    clickItem (id) {
      // 点击进入详情页
      this.$router.push({
        path: '/detail',
        query: {
          id
        }
      })
    },
    switchTab (index) {
      // 切换分类
      this.activeIndex = index
      this.fetchItems()
    },
    fetchItems () {
      // 请求对应分类商品
      fetchItems({
        categoryId: this.cates[this.activeIndex].id
      }).then((res) => {
        if (res.data.code === 0) {
          this.items = res.data.data.result
        }
        if (res.data.code === 700) {
          this.items = []
        }
      })
    },
    fetchCates () {
      fetchCates().then((res) => {
        if (res.data.code === 0) {
          this.cates = res.data.data
          // 商品初始调用
          this.fetchItems()
        }
      })
    }
  }
}
</script>

<style lang="scss" scoped>
.search-container {
  height: 50px;
  background-color: #fff;
  display: flex;
  align-items: center;
  justify-content: center;
  .search {
    display: flex;
    align-items: center;
    height: 40px;
    background-color: #eee;
    width: 96%;
    border-radius: 20px;
    color: #666;
    /deep/ .icon {
      margin: 0 10px;
    }
  }
}
.cate-container {
  position: fixed;
  left: 0;
  right: 0;
  bottom: 50px;
  top: 50px;
  background-color: rgb(174, 175, 79);
  display: flex;
  background-color: #fff;
  .cates-wrap {
    width: 2.5rem;
    height: 100%;
    overflow: auto;
    .cate {
      line-height: 40px;
      font-size: 14px;
      text-align: center;
      color: #333;
      position: relative;
      &.active {
        color: #ff734c;
        &:after {
          content: "";
          width: 2px;
          height: 34px;
          position: absolute;
          left: 0;
          top: 3px;
          background-color: #ff734c;
        }
      }
    }
  }
  .items-wrap {
    width: 7.5rem;
    height: 100%;
    overflow: auto;
    .item{
    display: flex;
    background-color: #fff;
    border-radius: 5px;
    margin-bottom: 10px;
    .item-img{
      width: 3rem;
    }
    .item-info{
      width: 4.5rem;
      padding: 0 10px;
      box-sizing: border-box;
      .item-name{
        font-size: 16px;
        margin-top: 8px;
        color: #333;
      }
      .item-desc{
        width: 4rem;
        font-size: 14px;
        line-height: 18px;
        margin: 10px 0;
        color: #333;
        white-space: nowrap;
        overflow: hidden;
        text-overflow: ellipsis;
      }
      .item-buy{
        display: flex;
        justify-content: space-between;
        .price{
          font-size: 20px;
          color: #ff734c;
        }
      }
    }
  }
  }
}
</style>
